package com.jh3502.helper.view;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

/**
 * @author harisucici
 */
public class MyScrollView extends NestedScrollView {
    public ViewGroup parentView;
    private boolean scroll = true;
    private int currentY;

    public MyScrollView(@NonNull Context context, AttributeSet attrs) {
        super(context, attrs);
    }


    public void setScroll(boolean b) {
        this.scroll = b;
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        if (parentView == null) {
            return super.onInterceptTouchEvent(ev);
        } else {
            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                // 将父scrollview的滚动事件拦截
                currentY = (int) ev.getY();
                setParentScrollAble(false);
                return super.onInterceptTouchEvent(ev);
            } else if (ev.getAction() == MotionEvent.ACTION_UP) {
                // 把滚动事件恢复给父Scrollview
                setParentScrollAble(true);
            } else if (ev.getAction() == MotionEvent.ACTION_MOVE) {
            }
        }
        return super.onInterceptTouchEvent(ev);

    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {

        View child = getChildAt(0);
        if (parentView != null) {
            if (ev.getAction() == MotionEvent.ACTION_MOVE) {
                int height = child.getMeasuredHeight();
                height = height - getMeasuredHeight();
                int scrollY = getScrollY();
                int y = (int) ev.getY();
                if (currentY < y) {
                    if (scrollY <= 0) {
                        // 如果向下滑动到头，就把滚动交给父Scrollview
                        Log.e("scroll", "down_1");
                        scroll = false;
                        //
                        setParentScrollAble(true);
                        return false;

                    } else {
                        Log.e("scroll", "down_0");
                        scroll = true;
                        //
                        setParentScrollAble(false);

                    }
                } else if (currentY > y) {
                    if (scrollY >= height) {
                        // 如果向上滑动到头，就把滚动交给父Scrollview
                        Log.e("scroll", "up_1");
                        scroll = false;
                        //
                        setParentScrollAble(true);
                        return false;
                    } else {
                        Log.e("scroll", "up_0");
                        scroll = true;
                        //
                        setParentScrollAble(false);
                    }

                }
                currentY = y;
            }
        }

        Log.e("scroll", "currentY=" + currentY);
        return super.onTouchEvent(ev);
    }

    private void setParentScrollAble(boolean flag) {
        parentView.requestDisallowInterceptTouchEvent(!flag);
    }
}
